package com.ruoyi.api.controller;

import com.ruoyi.api.domain.Products;
import com.ruoyi.api.service.IProductsService;
import com.ruoyi.api.service.IShoppingCartService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品表
 *
 * @author 寻觅
 * @since 2025-05-30
 */
@RestController
@RequestMapping("/products")
public class ProductsController extends BaseController {

    @Autowired
    IProductsService productsService;

    /**
     * 查询详情
     */
    @GetMapping("/info")
    public AjaxResult info(@RequestParam Long id) {
        return AjaxResult.success(productsService.info(id));
    }

    /**
     * 商品列表
     */
    @GetMapping("/products/list")
    public TableDataInfo productsInfo(Long id, Long userId, String productName) {
        startPage();
        return getDataTable(productsService.productsInfo(id, userId, productName));
    }

    /**
     * 商品列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Products products) {
        startPage();
        List<Products> products1 = productsService.listData(products);
        return getDataTable(products1);
    }

    @Autowired
    RedisCache redisCache;

    /**
     * 将商品存到redis里面
     */
    private static final String PRODUCT_DATA = "products_data";

    /**
     * 存储redis
     */
    @GetMapping("redir/lsit")
    public void redisList() {
//        先删除所有商品信息
        redisCache.deleteObject(PRODUCT_DATA);
        List<Products> list = productsService.list();
        for (Products products : list) {
            redisCache.setCacheMapValue(PRODUCT_DATA, products.getId().toString(), products);
            redisCache.setCacheMapValue(PRODUCT_DATA + "_stock",
                    products.getId().toString(),
                    products.getStockQuantity());
        }

    }
}
